/*******************************************************************/
/* @ All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.        */
/*                                                                 */
/*  NAME      = t__cics_pkt_is_fragmented.c                        */
/*  NOTE      =                                                    */
/*  DATE      = 2016/02/19 by songbb                               */
/*******************************************************************/

#include <CUnit/Basic.h>

#include "frcomm.h"
#include "frmem.h"

#include <cics/config.h>
#include <cics/cics_core.h>
#include <cics/cics_packet.h>
#include <apmtest_comm.h>

extern void __cics_destroy_packet(L7_cics_packet **pp_st_cics_packet);
extern int __cics_pkt_is_fragmented(L7_cics_packet *p_st_cics_packet
                                    , char *p_data
                                    , unsigned int ui_len
                                    , unsigned int *p_ui_len_read
                                    , unsigned int *p_ui_len_to_read);

static int __suite_init(void)
{
    return 0;
}

static int __suite_cleanup(void)
{
    return 0;
}

/**
 * 正常测试
 */
static void __case1(void)
{
    L7_cics_packet *p_st_cics_packet = NULL;
    p_st_cics_packet = (L7_cics_packet *) frmalloc(sizeof(L7_cics_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_cics_packet);
    memset(p_st_cics_packet, 0x00, sizeof(L7_cics_packet));

    CU_ASSERT_EQUAL(cics_init_packet(p_st_cics_packet), FR_SUCC);

    char *p_bin = (char *) "47617465007010000000000500000000000000000000000442415345000000001600027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__cics_pkt_is_fragmented(p_st_cics_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_CICS_PACKET_UNFRAGMENTED);
    CU_ASSERT_EQUAL(ui_len_read, strlen(p_bin) / 2);
    CU_ASSERT_EQUAL(ui_len_to_read, 0);

    __cics_destroy_packet(&p_st_cics_packet);
    CU_ASSERT_PTR_NULL(p_st_cics_packet);
}

/**
 * 正常测试
 */
static void __case2(void)
{
    L7_cics_packet *p_st_cics_packet = NULL;
    p_st_cics_packet = (L7_cics_packet *) frmalloc(sizeof(L7_cics_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_cics_packet);
    memset(p_st_cics_packet, 0x00, sizeof(L7_cics_packet));

    CU_ASSERT_EQUAL(cics_init_packet(p_st_cics_packet), FR_SUCC);

    char *p_bin = (char *) "47617465007010000000000500000000000000000000000442415345000000001700027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__cics_pkt_is_fragmented(p_st_cics_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_CICS_PACKET_FRAGMENTED);
    CU_ASSERT_EQUAL(ui_len_read, strlen(p_bin) / 2);
    CU_ASSERT_EQUAL(ui_len_to_read, 1);

    __cics_destroy_packet(&p_st_cics_packet);
    CU_ASSERT_PTR_NULL(p_st_cics_packet);
}

/**
 * 异常测试
 */
static void __case3(void)
{
    L7_cics_packet *p_st_cics_packet = NULL;
    p_st_cics_packet = (L7_cics_packet *) frmalloc(sizeof(L7_cics_packet));
    CU_ASSERT_PTR_NOT_NULL(p_st_cics_packet);
    memset(p_st_cics_packet, 0x00, sizeof(L7_cics_packet));

    CU_ASSERT_EQUAL(cics_init_packet(p_st_cics_packet), FR_SUCC);

    char *p_bin = (char *) "47617465007010000000000500000000000000000000000442415345000000001500027a680002434e0000000000000000000000000000";
    char arr_payload[65536] = {'\0'};

    CU_ASSERT_EQUAL(apmtest_hex_string_to_bin(p_bin, arr_payload, sizeof(arr_payload)), FR_SUCC);

    unsigned int ui_len_read = 0;       // 当前读了多少个字节
    unsigned int ui_len_to_read = 0;    // 还需要读多少个字节

    CU_ASSERT_EQUAL(__cics_pkt_is_fragmented(p_st_cics_packet, arr_payload, strlen(p_bin) / 2, &ui_len_read, &ui_len_to_read), APM_CICS_PACKET_ERROR);

    __cics_destroy_packet(&p_st_cics_packet);
    CU_ASSERT_PTR_NULL(p_st_cics_packet);
}

int add_suite__cics_pkt_is_fragmented(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite(MODULE_NAME" __cics_pkt_is_fragmented()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    if (CU_add_test(pSuite, "case1", __case1) == NULL ||
        CU_add_test(pSuite, "case2", __case2) == NULL ||
        CU_add_test(pSuite, "case3", __case3) == NULL)
        return FR_FAIL;

    return FR_SUCC;
}
